package vben.base.sys.perm.role;

import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Component;
import vben.common.jdbc.dto.PageData;
import vben.common.jdbc.dto.SidName;
import vben.common.jdbc.sqler.Isqler;
import vben.common.jdbc.sqler.JdbcHelper;
import vben.common.jdbc.sqler.Sqler;
import vben.common.jdbc.sqler.Usqler;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Component
@RequiredArgsConstructor
public class SysPermRoleDao {

    private final JdbcHelper jdbcHelper;

    public List<MenuVo> findMenuVoList() {
        String menuSql="select id,name,pid,icon,type from sys_perm_menu where avtag=1 order by ornum";
        List<MenuVo> menus= jdbcHelper.getTp().query(menuSql, new BeanPropertyRowMapper<>(MenuVo.class));

        String apiSql="select id,name,menid from sys_perm_api where avtag=1 order by ornum";
        List<ApiVo> apis= jdbcHelper.getTp().query(apiSql, new BeanPropertyRowMapper<>(ApiVo.class));

        for (MenuVo menu : menus) {
            for (ApiVo api : apis) {
                if(Objects.equals(api.getMenid(), menu.getId())) {
                    menu.getApis().add(api);
                }
            }
        }
        return menus;
    }

    public PageData findPageData(Sqler sqler) {
        return jdbcHelper.findPageData(sqler);
    }

    public SysPermRole findById(Long id) {
        String sql = "select * from sys_perm_role where id = ?";
        SysPermRole role = jdbcHelper.getTp().queryForObject(sql, new BeanPropertyRowMapper<>(SysPermRole.class), id);
        List<Long> apiidList = jdbcHelper.findLlist("select aid id from sys_perm_role_api where rid=?", id);
        role.setApis(apiidList);
        List<Long> menidList = jdbcHelper.findLlist("select mid id from sys_perm_role_menu where rid=?", id);
        role.setMenus(menidList);
        String orgSql="select t.id,t.name from sys_org t inner join sys_perm_role_org o on o.oid=t.id where o.rid=?";
        List<SidName> orgList = jdbcHelper.getTp().query(orgSql, new BeanPropertyRowMapper<>(SidName.class),id);
        role.setOrgs(orgList);
        return role;
    }

    public void insert(SysPermRole role) {
        Isqler isqler = new Isqler("sys_perm_role");
        isqler.add("id", role.getId());
        isqler.add("name", role.getName());
        isqler.add("notes", role.getNotes());
        isqler.add("crtim", role.getCrtim());
        isqler.add("uptim", role.getCrtim());
        isqler.add("avtag", role.getAvtag());
        isqler.add("ornum", role.getOrnum());
        jdbcHelper.getTp().update(isqler.getSql(), isqler.getParams());

        String apiSql = "insert into sys_perm_role_api(rid,aid) values(?,?)";
        List<Object[]> apiInsertList = new ArrayList<>();
        for (Long aid : role.getApis()) {
            Object[] arr = new Object[2];
            arr[0] = role.getId();
            arr[1] = aid;
            apiInsertList.add(arr);
        }
        jdbcHelper.batch(apiSql, apiInsertList);

        String menuSql = "insert into sys_perm_role_menu(rid,mid) values(?,?)";
        List<Object[]> menuInsertList = new ArrayList<>();
        for (Long mid : role.getMenus()) {
            Object[] arr = new Object[2];
            arr[0] = role.getId();
            arr[1] = mid;
            menuInsertList.add(arr);
        }
        jdbcHelper.batch(menuSql, menuInsertList);

        String orgSql = "insert into sys_perm_role_org(rid,oid) values(?,?)";
        List<Object[]> orgInsertList = new ArrayList<>();
        for (SidName sidName : role.getOrgs()) {
            Object[] arr = new Object[2];
            arr[0] = role.getId();
            arr[1] = sidName.getId();
            orgInsertList.add(arr);
        }
        jdbcHelper.batch(orgSql, orgInsertList);
    }

    public void update(SysPermRole role) {
        Usqler usqler = new Usqler("sys_perm_role");
        usqler.addWhere("id=?", role.getId());
        usqler.add("name", role.getName());
        usqler.add("notes", role.getNotes());
        usqler.add("uptim", role.getUptim());
        usqler.add("avtag", role.getAvtag());
        usqler.add("ornum", role.getOrnum());
        jdbcHelper.getTp().update(usqler.getSql(), usqler.getParams());

        jdbcHelper.update("delete from sys_perm_role_api where rid=?", role.getId());
        String apiSql = "insert into sys_perm_role_api(rid,aid) values(?,?)";
        List<Object[]> apiInsertList = new ArrayList<>();
        for (Long aid : role.getApis()) {
            Object[] arr = new Object[2];
            arr[0] = role.getId();
            arr[1] = aid;
            apiInsertList.add(arr);
        }
        jdbcHelper.batch(apiSql, apiInsertList);

        jdbcHelper.update("delete from sys_perm_role_menu where rid=?", role.getId());
        String menuSql = "insert into sys_perm_role_menu(rid,mid) values(?,?)";
        List<Object[]> menuInsertList = new ArrayList<>();
        for (Long mid : role.getMenus()) {
            Object[] arr = new Object[2];
            arr[0] = role.getId();
            arr[1] = mid;
            menuInsertList.add(arr);
        }
        jdbcHelper.batch(menuSql, menuInsertList);

        jdbcHelper.update("delete from sys_perm_role_org where rid=?", role.getId());
        String orgSql = "insert into sys_perm_role_org(rid,oid) values(?,?)";
        List<Object[]> orgInsertList = new ArrayList<>();
        for (SidName sidName : role.getOrgs()) {
            Object[] arr = new Object[2];
            arr[0] = role.getId();
            arr[1] = sidName.getId();
            orgInsertList.add(arr);
        }
        jdbcHelper.batch(orgSql, orgInsertList);
    }

    public void deleteById(Long id) {
        jdbcHelper.update("delete from sys_perm_role_org where rid=?", id);
        jdbcHelper.update("delete from sys_perm_role_api where rid=?", id);
        jdbcHelper.update("delete from sys_perm_role_menu where rid=?", id);
        jdbcHelper.update("delete from sys_perm_role where id=?", id);
    }

}
